package lium.lbs.lbsim.jchat;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetGroupIDListCallback;
import cn.jpush.im.android.api.callback.GetUserInfoListCallback;
import cn.jpush.im.android.api.event.MessageEvent;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import com.buz.hjcdriver.bean.SystemMessageBean;
import com.cjt2325.cameralibrary.util.LogUtil;
import com.lmlibrary.Constants;
import lium.lbs.lbsim.R;
import lium.lbs.lbsim.activity.MainActivity;
import lium.lbs.lbsim.fragments.ConversationListFragment;
import lium.lbs.lbsim.jchat.utils.ThreadUtil;
import lium.lbs.lbsim.utils.MsgMenuPopWindow;

import java.util.List;

/**
 * Created by ${chenyn} on 2017/3/13.
 */

public class ConversationListView {
    private View mConvListFragment;
    private ListView mConvListView = null;
    private TextView mTitle;
    public ImageView mCreateGroup;
    private LinearLayout mSearchHead;
    private LinearLayout mHeader;
    private RelativeLayout mLoadingHeader;
    private ImageView mLoadingIv;
    private LinearLayout mLoadingTv;
    private Context mContext;
    private TextView mNull_conversation;
    private LinearLayout mSearch;
    private TextView mAllUnReadMsg;
    private ConversationListFragment mFragment;
    public LinearLayout mSystemMessageHead;
    private static TextView all_unread_number;
    private static TextView tv_time;
    private static TextView tv_sm;
    public MsgMenuPopWindow msgMenuPopWindow;

    public ConversationListView(View view, Context context, ConversationListFragment fragment) {
        this.mConvListFragment = view;
        this.mContext = context;
        this.mFragment = fragment;
    }

    public void initModule() {
        mConvListView = (ListView) mConvListFragment.findViewById(R.id.conv_list_view);
        mCreateGroup = (ImageView) mConvListFragment.findViewById(R.id.create_group_btn);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mHeader = (LinearLayout) inflater.inflate(R.layout.conv_list_head_view, mConvListView, false);
        mSearchHead = (LinearLayout) inflater.inflate(R.layout.conversation_head_view, mConvListView, false);
        mLoadingHeader = (RelativeLayout) inflater.inflate(R.layout.jmui_drop_down_list_header, mConvListView, false);
        mSystemMessageHead = (LinearLayout) inflater.inflate(R.layout.conv_list_head_view_sm, mConvListView, false);
        mLoadingIv = (ImageView) mLoadingHeader.findViewById(R.id.jmui_loading_img);
        mLoadingTv = (LinearLayout) mLoadingHeader.findViewById(R.id.loading_view);
        mSearch = (LinearLayout) mSearchHead.findViewById(R.id.search_title);
        mNull_conversation = (TextView) mConvListFragment.findViewById(R.id.null_conversation);
        mAllUnReadMsg = (TextView) mFragment.getActivity().findViewById(R.id.all_unread_number);

        all_unread_number = (TextView) mSystemMessageHead.findViewById(R.id.all_unread_number);
        tv_time = (TextView) mSystemMessageHead.findViewById(R.id.tv_time);
        tv_sm = (TextView) mSystemMessageHead.findViewById(R.id.tv_sm);

        mConvListView.addHeaderView(mLoadingHeader);
        mConvListView.addHeaderView(mSearchHead);
        mConvListView.addHeaderView(mHeader);
        mConvListView.addHeaderView(mSystemMessageHead);
    }

    public void setConvListAdapter(ListAdapter adapter) {
        mConvListView.setAdapter(adapter);
    }


    public void setListener(View.OnClickListener onClickListener) {
        mSearch.setOnClickListener(onClickListener);
        mCreateGroup.setOnClickListener(onClickListener);
    }

    public void setItemListeners(AdapterView.OnItemClickListener onClickListener) {
        mConvListView.setOnItemClickListener(onClickListener);
    }

    public void setLongClickListener(AdapterView.OnItemLongClickListener listener) {
        mConvListView.setOnItemLongClickListener(listener);
    }


    public void showHeaderView() {
        mHeader.findViewById(R.id.network_disconnected_iv).setVisibility(View.VISIBLE);
        mHeader.findViewById(R.id.check_network_hit).setVisibility(View.VISIBLE);
    }

    public void dismissHeaderView() {
        mHeader.findViewById(R.id.network_disconnected_iv).setVisibility(View.GONE);
        mHeader.findViewById(R.id.check_network_hit).setVisibility(View.GONE);
    }


    public void showLoadingHeader() {
        mLoadingIv.setVisibility(View.VISIBLE);
        mLoadingTv.setVisibility(View.VISIBLE);
        AnimationDrawable drawable = (AnimationDrawable) mLoadingIv.getDrawable();
        drawable.start();
    }

    public void dismissLoadingHeader() {
        mLoadingIv.setVisibility(View.GONE);
        mLoadingTv.setVisibility(View.GONE);
    }

    public void setNullConversation(boolean isHaveConv) {
        if (mNull_conversation == null) {
            mNull_conversation = (TextView) mConvListFragment.findViewById(R.id.null_conversation);
        }
        if (isHaveConv) {
            mNull_conversation.setVisibility(View.GONE);
        } else {
            mNull_conversation.setVisibility(View.VISIBLE);
        }
    }

    public void showPopWindow() {
        if (msgMenuPopWindow == null) {
            msgMenuPopWindow = new MsgMenuPopWindow(mFragment.getActivity());
            msgMenuPopWindow.getContentView().findViewById(R.id.allclear).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mFragment.mConvListController.detelAll();
                    mFragment.mConvListController.mListAdapter.notifyDataSetChanged();
                    msgMenuPopWindow.dismiss();
                }
            });
            msgMenuPopWindow.getContentView().findViewById(R.id.allread).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < JMessageClient.getConversationList().size() - 1; i++) {
                        JMessageClient.getConversationList().get(i).setUnReadMessageCnt(0);
                        LogUtil.e("setUnReadMessageCnt" + JMessageClient.getConversationList().get(i).getUnReadMsgCnt());
                        for (int j = 0; j < JMessageClient.getConversationList().get(i).getAllMessage().size() - 1; j++) {
                            JMessageClient.getConversationList().get(i).getAllMessage().get(j).getContent();
                        }

                    }
                    mFragment.dapterNotify();
                    msgMenuPopWindow.dismiss();
                }
            });
        }
    }

    public void setUnReadMsg(final int count) {
        ThreadUtil.runInUiThread(new Runnable() {
            @Override
            public void run() {
                if (mAllUnReadMsg != null) {
                    if (count > 0) {
                        mAllUnReadMsg.setVisibility(View.VISIBLE);
                        if (count < 100) {
                            mAllUnReadMsg.setText(count + "");
                        } else {
                            mAllUnReadMsg.setText("99+");
                        }
                    } else {
                        mAllUnReadMsg.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    public static void setData(SystemMessageBean gRBean) {
        if (TextUtils.isEmpty(gRBean.getUnReadCount())) {
            all_unread_number.setVisibility(View.GONE);

            if (TextUtils.isEmpty(gRBean.getAddTime())) {
                tv_time.setVisibility(View.GONE);
            } else {
                tv_time.setText(gRBean.getAddTime());
                tv_time.setVisibility(View.VISIBLE);
            }
        } else {
            if (gRBean.getUnReadCount().equals("0")) {
                all_unread_number.setVisibility(View.GONE);
            } else {
                all_unread_number.setVisibility(View.VISIBLE);
                all_unread_number.setText(gRBean.getUnReadCount());
            }
        }
        if (TextUtils.isEmpty(gRBean.getRole())) {
            tv_sm.setVisibility(View.GONE);
        } else {
            tv_sm.setVisibility(View.VISIBLE);
            switch (gRBean.getRole()) {
                case "1"://我申请入群{gRBean.getGroupName()}
                    tv_sm.setText(gRBean.getMNickName() +"申请入" + gRBean.getGroupName() +"群");
                    break;
                case "2"://{getNickName}邀请我加入群{getGroupName}
                    tv_sm.setText(gRBean.getMNickName() + "邀请我加入群" + gRBean.getGroupName());
                    break;
                case "3"://我邀请{getNickName}加入群{getGroupName}
                    tv_sm.setText("我邀请" + gRBean.getMNickName() + "加入群" + gRBean.getGroupName());
                    break;
                case "4"://好友{getNickName}申请加我
                    tv_sm.setText("好友" + gRBean.getMNickName() + "申请加我");

                    break;

            }
            tv_time.setText(gRBean.getAddTime());
        }
    }

}
